package 树;

public class Leedoce617合并二叉树 {
    /**
     * 给你两个树根节点，就可以将两棵树合并为一棵树，如果一个树为空，另一个树对应的节点不为空，直接覆盖，否则，相加后覆盖
     * @param root1
     * @param root2
     * @return
     */
    TreeNode root;
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        //边界条件，
        if (root1 == null ){
            root = root2;
            return root;
        }
        if (root2 == null){
            root = root1;
            return root;
        }
        //走到这里说明两个节点都不为空
        root.val = root1.val + root2.val;
        root.left = mergeTrees(root1.left,root2.left);
        root.right = mergeTrees(root1.right,root2.right);
        return root;
    }
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
